-- ########################
-- Some misc stuff
local ClassButtons = CreateFrame('Frame', 'ClassButtons', UIParent)
local ClassButton = {}
local interval, interval2, timer = 0, 0, 0
local class = select(2, UnitClass("player"))
ClassButtons_OptionsLoaded = false

CurrentClassButtonsSpells = ClassButtonsSpells[class]
CurrentClassButtonsGlyphs = ClassButtonsGlyphs[class]
CurrentSpellsWithCounts = ClassButtonsSpellsWithCounts[class]

-- #################################
-- Create two secure frames for which will be the parents of the buttons normally, unless using stealth mode
CreateFrame("Button", "ClassButtons_VBarParent", UIParent)
CreateFrame("Button", "ClassButtons_HBarParent", UIParent)

-- Create the stealth mode parent
local f = CreateFrame("Button", "ClassButtons_ParentStealthMode", UIParent, "SecureHandlerClickTemplate")

f:SetAttribute('_onclick', [[
	if self:IsShown() then
		self:Hide()
	else
		self:Show()
	end
]])
f:Hide()

BINDING_HEADER_CLASSBUTTONS_HEADER = "Class Buttons"
_G["BINDING_NAME_CLICK ClassButtons_ParentStealthMode:LeftButton"] = "Toggle stealth window"
f:RegisterForClicks("AnyUp")

local f = CreateFrame("Frame", "ClassButtons_Stealth", ClassButtons_ParentStealthMode)
f:SetWidth(15); f:SetHeight(15)
f:SetPoint("TOPLEFT", UIParent, "CENTER", -35, 35)
--[[
local t = f:CreateTexture(nil,"BACKGROUND")
	t:SetTexture("Interface\\Addons\\ClassButtons\\gfx\\draghandle.tga")
	t:SetAllPoints(ClassButtons_Stealth)
	ClassButtons_Stealth.texture = t

f:SetScript("OnMouseDown", function() this:StartMoving(); this:SetUserPlaced(true) end)
f:SetScript("OnMouseUp", function() this:StopMovingOrSizing(); this:SetUserPlaced(true) end)
f:SetScript("OnDragStop", function() this:StopMovingOrSizing(); this:SetUserPlaced(true) end)
f:SetScript("OnEnter", function()
	GameTooltip:SetOwner(this, "ANCHOR_CURSOR")
	GameTooltip:SetText("Class Buttons stealth anchor:\n Left-click to drag")
end)
f:SetScript("OnLeave", function() GameTooltip:Hide(); this:SetUserPlaced(true) end)
f:EnableMouse(true); f:SetMovable(true)
]]
-- ######################################
-- Create the Anchor frames
for i = 1, 2 do
	local f = CreateFrame("Button", "ClassButtonsAnchor"..i, UIParent)
	f:SetWidth(15); f:SetHeight(15)
	f:SetPoint("TOPLEFT", UIParent, "CENTER", -35, 35)
	f:RegisterForClicks("AnyUp")
	f:SetMovable(true)
	
	local t = f:CreateTexture(nil,"BACKGROUND")
		t:SetTexture("Interface\\Addons\\ClassButtons\\gfx\\draghandle.tga")
		t:SetAllPoints(f)
		f.texture = t

	f:SetScript("OnMouseDown", function() this:StartMoving(); this:SetUserPlaced(true) end)
	f:SetScript("OnMouseUp", function() this:StopMovingOrSizing(); this:SetUserPlaced(true) end)
	f:SetScript("OnDragStop", function() this:StopMovingOrSizing(); this:SetUserPlaced(true) end)
	f:SetScript("OnEnter", function()
		GameTooltip:SetOwner(this, "ANCHOR_CURSOR")
		GameTooltip:SetText("Class Buttons anchor "..i..":\n Left-click to drag\n Shift-click to hide\n Ctrl-click to easily position and hide")
	end)
	f:SetScript("OnLeave", function() GameTooltip:Hide(); this:SetUserPlaced(true) end)
	f:SetScript("PostClick", function()
		if IsShiftKeyDown() then
			ClassButtonsDB.Anchor = false
			ClassButtonsAnchor1:EnableMouse(false)
			ClassButtonsAnchor2:EnableMouse(false)
			ClassButtonsAnchor1:SetAlpha(0)
			ClassButtonsAnchor2:SetAlpha(0)
			this:StopMovingOrSizing(); this:SetUserPlaced(true)	
		elseif IsControlKeyDown() then
			ClassButtonsDB.Anchor = false
			
			f:ClearAllPoints()
			f:SetPoint("LEFT", "MultiBarLeftButton1", "LEFT", -38, 41)
			f:SetUserPlaced(true)			
			f:EnableMouse(false)
			f:SetAlpha(0)
			this:StopMovingOrSizing(); this:SetUserPlaced(true)	
		end
	end)
end

-- ######################################
-- Register all events which I will use
ClassButtons:RegisterEvent("CHARACTER_POINTS_CHANGED")
ClassButtons:RegisterEvent("PLAYER_ENTERING_WORLD")
ClassButtons:RegisterEvent("ADDON_LOADED")
ClassButtons:RegisterEvent("LEARNED_SPELL_IN_TAB")
ClassButtons:RegisterEvent("PLAYER_TALENTS_UPDATED")
ClassButtons:SetScript("OnEvent", function() ClassButtons:OnEvent(event, arg1) end)

-- ####################
-- Event handler
function ClassButtons:OnEvent(event, arg1)
	if (event == "ADDON_LOADED" and arg1 == "ClassButtons") then
		SlashCmdList["CLASSBUTTONS"] = function()
			InterfaceOptionsFrame_OpenToCategory('ClassButtons v3') -- Opens the frame to change addon options
		end
		SLASH_CLASSBUTTONS1 = "/classbuttons3"
		SLASH_CLASSBUTTONS2 = "/cb3"

		-- ##########################################
		-- Default user settings, will create any variables that are missing.
		-- This is great for adding new features
		ClassButtonsDB = ClassButtonsDB or {}
		if ClassButtonsDB.Anchor == nil then ClassButtonsDB.Anchor = true end
		if ClassButtonsDB.HorizontalBar == nil then ClassButtonsDB.HorizontalBar = true end
		if ClassButtonsDB.VerticalBar == nil then ClassButtonsDB.VerticalBar = true end
		if ClassButtonsDB.Tooltips == nil then ClassButtonsDB.Tooltips = false end
		if ClassButtonsDB.VerticalScale == nil then ClassButtonsDB.VerticalScale = 1 end
		if ClassButtonsDB.HorizontalScale == nil then ClassButtonsDB.HorizontalScale = 1 end
		if ClassButtonsDB.StealthMode == nil then ClassButtonsDB.StealthMode = false end
		if ClassButtonsDB.Scale == nil then
				_G["ClassButtonsAnchor1"]:ClearAllPoints(); _G["ClassButtonsAnchor1"]:SetPoint("LEFT", "MultiBarLeftButton1", "LEFT", -38, 39)
				_G["ClassButtonsAnchor2"]:ClearAllPoints(); _G["ClassButtonsAnchor2"]:SetPoint("RIGHT", "MultiBarBottomRightButton12", "RIGHT", 40, 42)
				_G["ClassButtonsAnchor1"]:SetUserPlaced(true); _G["ClassButtonsAnchor2"]:SetUserPlaced(true)
		end --自动位置

		if not ClassButtonsDB['Anchor'] then
			ClassButtonsAnchor1:SetAlpha(0)
			ClassButtonsAnchor2:SetAlpha(0)
			ClassButtonsAnchor1:EnableMouse(false)
			ClassButtonsAnchor2:EnableMouse(false)
		end

		if ClassButtonsDB.HorizontalBar == true then
			ClassButtons_HBarParent:Show()
			RegisterStateDriver(ClassButtons_HBarParent, "visibility", "[bonusbar:5] hide; show")
		else
			ClassButtons_HBarParent:Hide()
			UnregisterStateDriver(ClassButtons_HBarParent)
		end

		if ClassButtonsDB.VerticalBar == true then
			ClassButtons_VBarParent:Show()
			RegisterStateDriver(ClassButtons_VBarParent, "visibility", "[bonusbar:5] hide; show")
		else
			ClassButtons_VBarParent:Hide()
			UnregisterStateDriver(ClassButtons_VBarParent)
		end

		-- Inject 'basic' options
		ClassButtons:BasicOptions(self)

	elseif (event == "PLAYER_ENTERING_WORLD") then
		self:RefreshButtons()

	elseif (event == "CHARACTER_POINTS_CHANGED" or event == "LEARNED_SPELL_IN_TAB" or event == "PLAYER_TALENTS_UPDATED") then
		timer = 3

	end
end

-- ######################################
-- Used to help refresh buttons, to help prevent code duplication
function ClassButtons:RefreshButtons()
	self:Create(1, "ClassButtonsAnchor1")
	self:Create(2, "ClassButton1")
	self:Create(3, "ClassButton13")
	self:Create(4, "ClassButton25")
	self:Create(5, "ClassButtonsAnchor2")
	self:UpdateTooltipInformation()
	self:RefreshPos()
end

-- #####################################
-- Determine the stealth mode status, and configure if needed
function ClassButtons:RefreshPos()
	if ClassButtonsDB.StealthMode == true then
		for i = 1, 60 do
			local b = _G["ClassButton"..i]
			if b and b.UseIt == true then
				b:SetParent(ClassButtons_ParentStealthMode)
			end
		end
		UnregisterStateDriver(ClassButtons_VBarParent)
		UnregisterStateDriver(ClassButtons_HBarParent)
	else
		for i = 1, 48 do
			local b = _G["ClassButton"..i]
			if b and b.UseIt == true then
				b:SetParent(ClassButtons_VBarParent)
			end
		end
		for i = 49, 60 do
			local b = _G["ClassButton"..i]
			if b and b.UseIt == true then
				b:SetParent(ClassButtons_HBarParent)
			end
		end
	end

	local lastButton, lastAnchor = "ClassButtonsAnchor1", "ClassButtonsAnchor1"
	local anchored = false
	-- Vertical Bar 1
	for i = 1, 12 do
		local button = _G["ClassButton"..i]
		
		if button and button.UseIt == true  then
			button:SetScale(ClassButtonsDB.VerticalScale)
			button:ClearAllPoints()
			button:Show()

			if not anchored then
				button:SetPoint("TOP", lastButton, "BOTTOM", 0, -6)
				anchored = true; lastButton = button; lastAnchor = button
			else
				button:SetPoint("TOP", lastButton, "BOTTOM", 0, -6)
				lastButton = button
			end
		end
	end
	
	-- Vertical Bar 2
	anchored = false
	for i = 13, 24 do
		local button = _G["ClassButton"..i]
		
		if button and button.UseIt == true  then
			button:SetScale(ClassButtonsDB.VerticalScale)
			button:ClearAllPoints()
			button:Show()
		
			if not anchored then
				if lastAnchor == "ClassButtonsAnchor1" then
					button:SetPoint("TOP", lastButton, "BOTTOM", 0, -6)
					anchored = true; lastButton = button; lastAnchor = button
				else
					button:SetPoint("RIGHT", lastAnchor, "LEFT", -6, 0)
					anchored = true; lastButton = button; lastAnchor = button
				end
				
			else
				button:SetPoint("TOP", lastButton, "BOTTOM", 0, -6)
				lastButton = button
			end
		end
	end
	
	-- Vertical Bar 3
	anchored = false
	for i = 25, 36 do
		local button = _G["ClassButton"..i]
		
		if button and button.UseIt == true  then
			button:SetScale(ClassButtonsDB.VerticalScale)
			button:ClearAllPoints()
			button:Show()
		
			if not anchored then
				if lastAnchor == "ClassButtonsAnchor1" then
					button:SetPoint("TOP", lastButton, "BOTTOM", 0, -6)
					anchored = true; lastButton = button; lastAnchor = button
				else
					button:SetPoint("RIGHT", lastAnchor, "LEFT", -6, 0)
					anchored = true; lastButton = button; lastAnchor = button
				end			
			else
				button:SetPoint("TOP", lastButton, "BOTTOM", 0, -6)
				lastButton = button
			end
		end
	end
	
	-- Vertical Bar 4
	anchored = false
	for i = 37, 48 do
		local button = _G["ClassButton"..i]
		
		if button and button.UseIt == true  then
			button:SetScale(ClassButtonsDB.VerticalScale)
			button:ClearAllPoints()
			button:Show()
		
			if not anchored then
				if lastAnchor == "ClassButtonsAnchor1" then
					button:SetPoint("TOP", lastButton, "BOTTOM", 0, -6)
					anchored = true; lastButton = button; lastAnchor = button
				else
					button:SetPoint("RIGHT", lastAnchor, "LEFT", -6, 0)
					anchored = true; lastButton = button; lastAnchor = button
				end
			else		
				button:SetPoint("TOP", lastButton, "BOTTOM", 0, -6)
				lastButton = button
			end
		end
	end
	
	-- Horizontal bar
	anchored = false	
	for i = 49, 60 do
		local button = _G["ClassButton"..i]
		
		if button and button.UseIt == true then
			button:SetScale(ClassButtonsDB.HorizontalScale)
			button:ClearAllPoints()
			button:Show()
			
			if not anchored then
				button:SetPoint("RIGHT", "ClassButtonsAnchor2", "LEFT", -6, 0)
				anchored = true; lastButton = button			
			else
				button:SetPoint("RIGHT", lastButton, "LEFT", -6, 0)
				lastButton = button
			end
		end
	end
end

-- ######################################
-- Rescan the spellbook to get the latest tooltip information
function ClassButtons:UpdateTooltipInformation()
	for i = 1, 60 do
		local button = _G["ClassButton"..i]
		if button then
			local theSpell = button.TheSpell
			local highestRank = self:GetSpellID(theSpell)
			
			-- ON MOUSE OUT
			button:SetScript("OnLeave", function() GameTooltip:Hide() end)
			
			-- ON MOUSE IN
			button:SetScript("OnEnter", function()
				if (ClassButtonsDB['Tooltips'] == true) then
					GameTooltip:SetOwner(this, "ANCHOR_CURSOR")
					GameTooltip:SetSpell(highestRank, BOOKTYPE_SPELL)
					GameTooltip:Show()
				end
			end)
		end
	end
end

-- ###################################
-- A stand alone onUpdate to help with updating the button creation and layout, to prevent spam
local ClassButtons_OnUpdate = CreateFrame('Frame', 'ClassButtons_OnUpdate', UIParent)
ClassButtons_OnUpdate:SetScript("OnUpdate", function(self, elapsed)
	interval2 = interval2 - elapsed
	if interval2 <= 0 then

		if timer >= 0 then
			timer = timer - 1 or 0
			if timer == 0 then
				if InCombatLockdown("player") then timer = 3; return end
				ClassButtons:RefreshButtons()
			end
		end

		-- Changing interval will allow a delay between updates.
		interval2 = 1
	end
end)

-- ########################################################################
-- Check + Create the buttons after the player has leveled, trained spells, respecked, and loaded into the game if needed
function ClassButtons:Create(frameToBuild, startPoint)
    local frame = 0

    -- This is to prevent an error returned by classes that do not have any spells
	if not CurrentClassButtonsSpells[frameToBuild] then return end

    for i = 1, self:countEntries(CurrentClassButtonsSpells[frameToBuild]) do
		if frameToBuild == 1 then frame = i else frame = i + ((frameToBuild - 1) * 12) end

		local theSpell = CurrentClassButtonsSpells[frameToBuild][i]
		local button = _G["ClassButton"..frame]
		local SpellID = self:GetSpellID(theSpell)

        if SpellID then
			-- If the button exists, show it, if it does not exist yet, create it.
			-- This is fine, as the button # will _always_ refrence the exact same spell in the spell list, until the user changes it and reloads the ui
			if button then
				button.UseIt = true
				button:Show()
			else
				if frameToBuild == 5 then
					ClassButton[frame] = CreateFrame("Button", "ClassButton"..frame, ClassButtons_HBarParent,
						"ActionButtonTemplate, SecureActionButtonTemplate, SecureHandlerDragTemplate, SecureHandlerStateTemplate")
				else
					ClassButton[frame] = CreateFrame("Button", "ClassButton"..frame, ClassButtons_VBarParent,
						"ActionButtonTemplate, SecureActionButtonTemplate, SecureHandlerDragTemplate, SecureHandlerStateTemplate")
				end
				button = _G["ClassButton"..frame]
				button:SetWidth(36); button:SetHeight(36)
				button.TheSpell = theSpell
				button.UseIt = true
				button:SetAttribute("type", "spell"); button:SetAttribute("spell", theSpell)
				button:SetAttribute("modifiers", "SELFCAST:self")
				button:SetAttribute("self-unit*", "player")
				button:SetAttribute("_ondragstart", "return 'spell', self:GetAttribute('spell');")

				button:RegisterForDrag("LeftButton", "RightButton")
				button:RegisterForClicks("AnyUp")
				button:EnableMouse(true)

				button:SetScript('OnUpdate', function(self, elapsed) ClassButtons:OnUpdate(self, elapsed) end)
				button:RegisterEvent('SPELL_UPDATE_COOLDOWN')
				button:SetScript('OnEvent', function()
					local start, duration, enable = GetSpellCooldown(tostring(_G[this:GetName()].TheSpell))
					if start ~= nil and duration ~= nil then
						_G[this:GetName().."Cooldown"]:SetCooldown(start, duration)
					end
				end)

				-- SET THE BUTTONS TEXTURE
				local t = _G[button:GetName().."Icon"]
				t:SetTexture(GetSpellTexture(theSpell))
				t:SetPoint('Center', button)
				t:SetWidth(36); t:SetHeight(36)
				
				-- Set the item to be counted
				for i = 1, self:countEntries(CurrentSpellsWithCounts) do
					if theSpell == CurrentSpellsWithCounts[i][1] then
						if class == "DRUID" then
							local levels = { 20, 30, 40, 50, 60, 69, 79 }
							local playerLevel = UnitLevel("player")
							for j = 1, 7 do
								if levels[j] <= playerLevel then _G["ClassButton"..frame].CountNum = CurrentSpellsWithCounts[i][j+1] end
							end
						elseif class == "PRIEST" then
							local levels = { 48, 60, 70, 80 }
							local playerLevel = UnitLevel("player")
							for j = 1, 4 do
								if levels[j] <= playerLevel then _G["ClassButton"..frame].CountNum = CurrentSpellsWithCounts[i][j+1] end
							end
						else
							_G["ClassButton"..frame].CountNum = CurrentSpellsWithCounts[i][2]
						end
					end
				end
			end			
        else
			-- Hide the button if the spell is not on the spellbook
			if button then button.UseIt = false; button:Hide() end
		end
    end
end

-- #################################
-- Button update function
function ClassButtons:OnUpdate(self, elapsed)
	interval = interval - elapsed
	if interval <= 0 then
		-- #########################
		-- Count the reagents
		if this.CountNum ~= nil then
			local count = GetItemCount(this.CountNum)
			if ClassButtons:GlyphCheck(spellName) == true then count = "" end
			_G[this:GetName().."Count"]:SetText("|cffffffff"..count)
		end
		-- Changing interval will allow a delay between updates.
		interval = 1
	end

	-- #########################
	-- Colour the buttons ...
	-- thanks to ckknight & navcaririel for the line below!
	local num, batch = tonumber((this:GetName()):match("%d+")), 1
	local button = _G["ClassButton"..num.."Icon"]

	num, batch = num % 12, math.ceil(num / 12)

	local spellName = tostring(CurrentClassButtonsSpells[batch][num] or "")
	local usable, nomana = IsUsableSpell(spellName)

	if not button then return end

	-- CHANGE TO BLUE IF INSUFFICIENT MANA
	if nomana then
		button:SetVertexColor(.1, .3, 1)

	-- CHANGE TO RED IF OUT OF RANGE
	elseif IsSpellInRange(spellName, "target") == 0 then
		button:SetVertexColor(0.8, 0.1, 0.1)

	-- GREY OUT IF FLYING/DEAD/GHOST
	elseif UnitIsDeadOrGhost("player") or UnitOnTaxi("player") then
		button:SetVertexColor(.3, .3, .3)

	-- CLEAR
	else
		button:SetVertexColor(1, 1, 1)
	end
end

-- #############################
-- Used to count the total amount of entries in tbl
function ClassButtons:countEntries(tbl)
    local cnt = 0;
    for k in pairs(tbl) do cnt = cnt + 1; end
    return cnt
end

-- #################################################################################
-- Function to check the glyphs the player has installed. This will come in handy much more later when more - reagent costs are around
function ClassButtons:GlyphCheck(spellName)
	for i = 1, 6 do
		local PlayerGlyphName = select(3, GetGlyphSocketInfo(i))
		if PlayerGlyphName ~= nil then
			for j = 1, self:countEntries(CurrentClassButtonsGlyphs) do
				if GetSpellInfo(PlayerGlyphName) == CurrentClassButtonsGlyphs[j][2] and spellName == CurrentClassButtonsGlyphs[j][1] then return true end
			end
		end
	end
end

-- #################################
-- Returns the SpellID of the highest 'spell' in your spellbook
function ClassButtons:GetSpellID(spell, rank)
	local i = 1
	local spellID
	local highestRank
	while true do
		local spellName, spellRank = GetSpellName(i, BOOKTYPE_SPELL)
		if (not spellName) then
			break
		end
		if (spellName == spell) then
			if (rank) then
				if (spellRank == rank) then
					return i
				end
			else
				spellID = i
				highestRank = spellRank
			end
		end
		i = i + 1
		if (i > 350) then
			break
		end
	end
	return spellID, highestRank
end

function ClassButtons:BasicOptions(self)
	local panel = CreateFrame("FRAME", "ClassButtons_Options_Panel")

	panel.name = "ClassButtons v3"

	panel.okay = function()
		ClassButtons:HideCheck()
		timer = 3

		for i, button in ipairs{panel:GetChildren()} do
			button.origValue = storage[field]
		end
	end

	panel.cancel = function()
		for i, button in ipairs{panel:GetChildren()} do
			if button.Restore then
				button:Restore()
			end
		end
	end

	panel.default = function()
		ClassButtonsDB.Anchor = true
		ClassButtonsDB.HorizontalBar = true
		ClassButtonsDB.VerticalBar = true
		ClassButtonsDB.Tooltips = true
		ClassButtonsDB.VerticalScale = 1
		ClassButtonsDB.HorizontalScale = 1
		ClassButtonsDB.StealthMode = false

		ClassButtonsAnchor1:ClearAllPoints(); ClassButtonsAnchor1:SetPoint("CENTER", "UIParent", "CENTER", 0, 0)
		ClassButtonsAnchor2:ClearAllPoints(); ClassButtonsAnchor2:SetPoint("CENTER", "UIParent", "CENTER", 0, 0)
		ClassButtonsAnchor1:SetUserPlaced(true); ClassButtonsAnchor2:SetUserPlaced(true)

		for i = 1, 48 do
			local frame = _G["ClassButton"..i]
			if frame then frame:SetScale(ClassButtonsDB.VerticalScale) end
		end

		for i = 49, 60 do
			local frame = _G["ClassButton"..i]
			if frame then frame:SetScale(ClassButtonsDB.HorizontalScale) end
		end

		InterfaceOptionsFrameOkay:Click()
	end

	local LoadMore = ClassButtons:CreateButton("Load More Options", panel, 155, 30)
	LoadMore:SetPoint('TOPLEFT', 10, -30)
	LoadMore:SetScript('OnClick', function()
		this:Hide()

		if ClassButtons_OptionsLoaded == false then
			UIParentLoadAddOn("ClassButtons_Options")	-- Loads the options addon into game memory
			self:LoadOptions(self) 						-- Creates the options frame in the Addons List
			ClassButtons_OptionsLoaded = true
		end
	end)

	InterfaceOptions_AddCategory(panel)
end

function ClassButtons:CreateButton(name, parent, width, height)
	local button = CreateFrame('Button', parent:GetName() .. name, parent, 'UIPanelButtonTemplate')
	button:SetWidth(width)
	button:SetHeight(height)

	local frame = _G[button:GetName() .. 'Text']
	frame:SetText(name)
	frame:SetTextColor(1, 1, 1, 1)

	return button
end

function ClassButtons:HideCheck()
	if ClassButtonsDB.Anchor == true then
		ClassButtonsAnchor1:SetAlpha(1)
		ClassButtonsAnchor2:SetAlpha(1)
		ClassButtonsAnchor1:EnableMouse(true)
		ClassButtonsAnchor2:EnableMouse(true)
	else
		ClassButtonsAnchor1:SetAlpha(0)
		ClassButtonsAnchor2:SetAlpha(0)
		ClassButtonsAnchor1:EnableMouse(false)
		ClassButtonsAnchor2:EnableMouse(false)
	end

	if ClassButtonsDB.HorizontalBar == true then
		ClassButtons_HBarParent:Show()
		RegisterStateDriver(ClassButtons_HBarParent, "visibility", "[bonusbar:5] hide; show")
	else
		ClassButtons_HBarParent:Hide()
		UnregisterStateDriver(ClassButtons_HBarParent)
	end

	if ClassButtonsDB.VerticalBar == true then
		ClassButtons_VBarParent:Show()
		RegisterStateDriver(ClassButtons_VBarParent, "visibility", "[bonusbar:5] hide; show")
	else
		ClassButtons_VBarParent:Hide()
		UnregisterStateDriver(ClassButtons_VBarParent)
	end
end

	--[[
		local lastButton, lastAnchor
		local b = 1

		if ClassButtonsDB.VerticalBar == true then
			for i = 1, 48 do
				local button = _G["ClassButton"..i]
				if button and button.UseIt == true then					
					button:SetScale(ClassButtonsDB.VerticalScale)
					if not lastButton then
						button:SetParent(ClassButtons_ParentStealthMode)
						button:ClearAllPoints(); button:SetPoint("TOPLEFT", ClassButtons_Stealth, "TOPLEFT", 17, 210)
						lastButton = button
						lastAnchor = button
					else
						if b == 5 then
							button:SetParent(ClassButtons_ParentStealthMode)
							button:ClearAllPoints(); button:SetPoint("LEFT", lastAnchor, "RIGHT", 6, 0)
							lastButton = button
							lastAnchor = button
							b = 1
						else
							button:SetParent(ClassButtons_ParentStealthMode)
							button:ClearAllPoints(); button:SetPoint("TOP", lastButton, "BOTTOM", 0, -6)
							lastButton = button
							b = b + 1
						end
					end
				end
			end
		end
		
		lastButton, lastAnchor, b = nil, nil, 1
		
		if ClassButtonsDB.HorizontalBar == true then
			for i = 49, 60 do
				local button = _G["ClassButton"..i]
				if button and button.UseIt == true then
					
					button:SetScale(ClassButtonsDB.HorizontalScale)
					if not lastButton then
						button:SetParent(ClassButtons_ParentStealthMode)
						button:ClearAllPoints(); button:SetPoint("TOPLEFT", ClassButtons_Stealth, "TOPLEFT", 17, -15)
						lastButton = button
						lastAnchor = button
					else
						if lastAnchor then
							button:SetParent(ClassButtons_ParentStealthMode)
							button:ClearAllPoints(); button:SetPoint("LEFT", lastAnchor, "RIGHT", 6, 0)
							lastButton = button
							lastAnchor = nil							
						else
							button:SetParent(ClassButtons_ParentStealthMode)
							button:ClearAllPoints(); button:SetPoint("LEFT", lastButton, "RIGHT", 6, 0)
							lastButton = button
						end
					end
				end
			end
		end
		
		]]
